package com.cuckoo.mall.thus.menuindex.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bgn.baseframe.base.BaseListFragment;
import com.bgn.baseframe.network.HttpObserver;
import com.bgn.baseframe.network.bean.TDataBean;
import com.bgn.baseframe.utils.decoration.TwoRowGridViewDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cuckoo.mall.R;
import com.cuckoo.mall.thus.manager.UserInfoManager;
import com.cuckoo.mall.javabean.Product;
import com.cuckoo.mall.network.ApiManager;
import com.cuckoo.mall.thus.menuindex.adapter.CategoriespProductListAdapter;

import java.util.List;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class CategoryProductListFragment extends BaseListFragment<ListContract.View, ListPresenter, Product> implements ListContract.View {

    public final static String ID = "id";
    private int category_id = 0;
    private ImageView ivTop;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) category_id = bundle.getInt(ID);
    }

    public static CategoryProductListFragment newInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt(ID, id);
        CategoryProductListFragment contentFragment = new CategoryProductListFragment();
        contentFragment.setArguments(bundle);
        return contentFragment;
    }

    @Override
    protected BaseQuickAdapter setAdapter() {
        adapter = new CategoriespProductListAdapter(datas, new CategoriespProductListAdapter.WhenPositionChange() {
            @Override
            public void changeImageViewVisibile(boolean isShow) {
                ivTop.setVisibility(isShow ? View.VISIBLE : View.GONE);
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build("/mall/goodMultipleDetailActivity").withString(ID, datas.get(position).getProduct_id()).navigation();
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(final BaseQuickAdapter adapter, View view, final int position) {
                if (datas.get(position).getIs_favorite() == 0 && UserInfoManager.getInstance().checkTrunLogin()) {
                    ApiManager.addFavorite(datas.get(position).getProduct_id()).subscribe(new HttpObserver<TDataBean>(getBaseActivity()) {
                        @Override
                        public void onSucceed(TDataBean value) {
                            datas.get(position).setIs_favorite(1);
                            adapter.notifyItemChanged(position);
                        }
                    });
                }
            }
        });
        return adapter;
    }

    @Override
    protected void initOtherUi(View rootView) {
        super.initOtherUi(rootView);
        //recycleView.addItemDecoration(new CommonDecoration(12));
        ivTop = rootView.findViewById(R.id.iv_top);
        ivTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recycleView.smoothScrollToPosition(0);
            }
        });
    }

    @Override
    protected void onRefreshData() {
        mPresenter.loadProductListById(HTTP_REFRESH_LIST, category_id, 0, page_count);
    }

    @Override
    protected void initListData() {
        mPresenter.loadProductListById(HTTP_INIT_LIST, category_id, page_index, page_count);
    }

    @Override
    protected void loadMoreData() {
        mPresenter.loadProductListById(HTTP_LOAD_MORE_LIST, category_id, page_index, page_count);
    }


    @Override
    public void whenGetDataBack(int type, List<Product> datas) {


        //处理  adapter设置空布局的情况下 gridview 双行 导致空布局 间距问题
        if (type == HTTP_INIT_LIST || type == HTTP_REFRESH_LIST) {
            if (recycleView.getItemDecorationCount() == 0 && datas != null && datas.size() > 0) {//无间距，数组不空   加间距

                recycleView.addItemDecoration(new TwoRowGridViewDecoration(12));
            } else if (recycleView.getItemDecorationCount() != 0 && (datas == null || datas.size() == 0)) {//有间距，数组空，移除间距
                recycleView.removeItemDecorationAt(0);
            }
        }

        handleData(datas, type);
    }

    @Override
    protected int getLaoutResouse() {
        return R.layout.fragment_index_list;
    }

    interface resetBackIcoCallBack {

    }

    @Override
    protected void setEmptyView() {
        if (shoudSetEmptyView()) {
            adapter.setEmptyView(R.layout.default_no_data);
        }
    }

    @Override
    protected boolean shouldRemoveNoMoreView() {
        return false;
    }

    @Override
    protected void setLayoutManage() {
        // recycleView.addItemDecoration(new TwoRowGridViewDecoration(12));
        final GridLayoutManager manager = new GridLayoutManager(getBaseActivity(), 2);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == adapter.getItemCount() - 1) {
                    return manager.getSpanCount();
                } else {
                    return 1;
                }
            }
        });
        recycleView.setLayoutManager(manager);
    }
}
